//201902104003邵潇漩
package util;

import com.alibaba.fastjson.JSONArray;

import java.util.List;

public class Condition {
    public static String toWhereClause(List<Condition> conditionList) {
        StringBuilder sb = new StringBuilder(" WHERE ");
        //将字段名、操作符和字段值依次拼接为字串
        for (Condition c : conditionList) {
            sb.append(c.getKey()).append(" ").append(c.getOperator()).append(" ");
            String value = c.getValue();
            //如果操作符是like，自动设置为模糊匹配
            if ("like".equalsIgnoreCase(c.getOperator())) {
                value = "%"+value+"%";
            }
            value = TxtHelper.quoteMarked(value);
            sb.append(value);
            sb.append(" AND ");
        }
        //把最后一个条件后面的“and”删除
        TxtHelper.truncEnding(sb, 5);
        return sb.toString();
    }

    //不能定义无参构造器，否则fastjson会使用它来创建对象
    public Condition(String value, String key, String operator) {
        this.key = key;
        this.operator = operator;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getOperator() {
        return operator;
    }

    public String getValue() {
        return value;
    }

    //字段名
    private String key;
    //操作符
    private String operator;
    //字段值
    private String value;
}